package com.tadpole.music.view.activity.study;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.study.ExaminationErrorAdapter;
import com.tadpole.music.adapter.study.QuestionErrorAdapter;
import com.tadpole.music.bean.study.MoNiErrorQuestionBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.study.MoNiErrorQuestionIView;
import com.tadpole.music.presenter.study.MoNiErrorQuestionPresenter;
import com.tadpole.music.presenter.study.MoNiErrorQuestionStudentPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.question.RealTrainActivity;

import java.util.List;

/**
 * 模拟训练错题列表
 * Created By 孙岩
 * on 2020/10/10
 */
public class QuestionErrorActivity extends BaseActivity implements MoNiErrorQuestionIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //刷新
    private SmartRefreshLayout rl_refresh;
    //recyclerView
    private RecyclerView recyclerView;
    //adapter
    private QuestionErrorAdapter questionErrorAdapter;
    //presenter
    private MoNiErrorQuestionPresenter moNiErrorQuestionPresenter;
    private MoNiErrorQuestionStudentPresenter moNiErrorQuestionStudentPresenter;
    //考试场次id
    private int id;
    //1表示自己 2表示学生
    private String flag;

    //学生id
    private String student_id;
    //学院id
    private String college_id;
    //学院名称
    private String name;
    //等级id
    private int rank_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_examination_error);
        initViews();
        initListeners();
        id = getIntent().getIntExtra("id",0);
        flag = getIntent().getStringExtra("flag");
        college_id = getIntent().getStringExtra("college_id");
        name = getIntent().getStringExtra("name");
        rank_id = getIntent().getIntExtra("rank_id",0);

        if("1".equals(flag)){//自己

        }else if("2".equals(flag)){//学生
            student_id = getIntent().getStringExtra("student_id");
        }

        moNiErrorQuestionPresenter = new MoNiErrorQuestionPresenter();
        moNiErrorQuestionPresenter.attachView(this);

        moNiErrorQuestionStudentPresenter = new MoNiErrorQuestionStudentPresenter();
        moNiErrorQuestionStudentPresenter.attachView(this);

        if("1".equals(flag)){//自己
            moNiErrorQuestionPresenter.getMoNiErrorQuestionList(id,false);
        }else if("2".equals(flag)){//学生
            moNiErrorQuestionStudentPresenter.getMoNiErrorQuestionList(id,student_id,false);
        }

    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("训练错题");
        rl_refresh = findViewById(R.id.rl_refresh);
        recyclerView = findViewById(R.id.recyclerView);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                QuestionErrorActivity.this.finish();
            }
        });

        //添加刷新监听器
        rl_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                if("1".equals(flag)) {//自己
                    moNiErrorQuestionPresenter.getMoNiErrorQuestionList(id,true);
                }else if("2".equals(flag)){
                    moNiErrorQuestionStudentPresenter.getMoNiErrorQuestionList(id,student_id,true);
                }
            }
        });

        //上拉加载
        rl_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@androidx.annotation.NonNull RefreshLayout refreshLayout) {
                if("1".equals(flag)) {//自己
                    moNiErrorQuestionPresenter.getMoNiErrorQuestionListMore(id);
                }else if("2".equals(flag)){
                    moNiErrorQuestionStudentPresenter.getMoNiErrorQuestionListMore(id,student_id);
                }

            }
        });
    }


    @Override
    public void notifyAdapter() {
        questionErrorAdapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishRefresh();
        rl_refresh.finishLoadMore();
    }

    @Override
    public void showMoNiErrorQuestionList(final List<MoNiErrorQuestionBean.DataBeanX.DataBean> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        questionErrorAdapter = new QuestionErrorAdapter(this,list,flag);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(questionErrorAdapter);

        questionErrorAdapter.setOnChildClickListener(new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    if(v.getId()==R.id.tvSeeError){//查看错题
                        Intent intent = new Intent(QuestionErrorActivity.this,ErrorQuestionActivity.class);
                        intent.putExtra("id",list.get(position).getId());
                        intent.putExtra("error",list.get(position).getErr_selected());

                        startActivity(intent);
                    }else if(v.getId()==R.id.tvSelect){//重新做题
                        Intent intent = new Intent(QuestionErrorActivity.this, RealTrainActivity.class);
                        intent.putExtra("id",Integer.valueOf(college_id));
                        intent.putExtra("name",name);
                        intent.putExtra("rank_id",rank_id);
                        startActivity(intent);
                    }
                }else{
                    ToastUtils.show("请检查您的网路设置");
                }
            }
        });
    }

    @Override
    public void show401() {
        SpUtil.getInstance(this).putString(Constant.TOKEN, "");
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }
}
